package cn.whdreamblog.servers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.whdreamblog.dao.BlogArticleDAO;
import cn.whdreamblog.dao.MessageDAO;
import cn.whdreamblog.daoimp.BlogArticleDAOImp;
import cn.whdreamblog.daoimp.MessageDAOImp;
import cn.whdreamblog.entites.BlogArticle;
import cn.whdreamblog.entites.Message;
import cn.whdreamblog.entites.PageBean;
import cn.whdreamblog.iservers.IBlogArticleServers;
import cn.whdreamblog.iservers.IMessageServers;
import cn.whdreamblog.util.BlogUtil;


public class BlogArticleServers implements IBlogArticleServers{
	private BlogArticleDAO blogDAO;
	private IMessageServers msgDAO;
	public BlogArticleServers() {
		blogDAO=new BlogArticleDAOImp();
		msgDAO = new MessageServers();
	}
	
	public boolean addBlogArticle(BlogArticle ba) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		ba.setPubdate(sdf.format(System.currentTimeMillis()));
		return blogDAO.insertBlogArticle(ba);
	}
	
	public boolean removeBlogArticle(BlogArticle ba) {
		return blogDAO.deleteBlogArticle(ba);
	}
	
	public boolean updateBlogArticle(BlogArticle ba) {
		return blogDAO.updateBlogArticle(ba);
	}
	
	public PageBean searchBlogArticleByKind(String kind , int limit ,int page){
		PageBean p = blogDAO.searchBlogArticleByAttrAndPage(kind, limit , page);
		if(p == null){
			return null;
		}
		List<BlogArticle> temp = p.getList();
		p.setList(this.fomatListTime(temp));
		p.setList(BlogUtil.getTextFromMultitermHtmlLimtLength(temp , 200));
		return p; 
	};
	
	
	public BlogArticle searchBlogArticleById(int id){
		return blogDAO.searchBlogArticleById(id);
	}
	
	public Map<String, Object> getBlogArticleAndComments(int id){
		Map<String, Object> map = new HashMap<String, Object>();
		BlogArticle b= blogDAO.searchBlogArticleById(id);
		b.setPubdate(BlogUtil.fomat(b.getCreatTime()));
		map.put("blog", b);
		List<Message> list = msgDAO.getSomeMessages(0, Integer.MAX_VALUE, id);
		if(list != null&&list.size()>0){
		   map.put("comment", list);
		}else{
			list = new ArrayList<Message>();
			map.put("comment", list);
		}
		return map;
	}
	
	@Override
	public List<BlogArticle> getAllBlogArticles() {

		return blogDAO.getAllBlogArticles();
	}

	@Override
	public PageBean getAllBlogSomeArticles( int page, int limit,int length) {
		PageBean p =blogDAO.getAllBlogSomeArticlesByPage(limit, page);
		if(p == null){
			return null;
		}
		List<BlogArticle> temp = p.getList();
		p.setList(this.fomatListTime(temp));
		temp =length>10?BlogUtil.getTextFromMultitermHtmlLimtLength(temp , length)
				:BlogUtil.getTextFromMultitermHtmlLimtLength(temp , 200);
		p.setList(temp);
		return p;
	}
	
	@Override
	public List<BlogArticle> getAllBlogSomeArticles( int start, int limit) {
		List<BlogArticle> temp = blogDAO.getAllBlogSomeArticles(start,limit);
		return BlogUtil.getTextFromMultitermHtmlLimtLength(temp , 200);
	}
	

	@Override
	public List<BlogArticle> getSomeBlogArticlesAttribute(int start, int limit,
			String attr) {
		return blogDAO.getSomeBlogArticlesAttribute(start, limit, attr);
	}
	
	@Override
	public PageBean getAllBlogSomeArticlesByPage(int limit, int page) {
		PageBean p =blogDAO.getAllBlogSomeArticlesByPage(limit, page);
		if(p == null){
			return null;
		}
		List<BlogArticle> temp = p.getList();
		p.setList(this.fomatListTime(temp));
		p.setList(BlogUtil.getTextFromMultitermHtmlLimtLength(temp , 200));
		return p;
	};
	private List fomatListTime(List<BlogArticle> list){
		for (BlogArticle blogArticle : list) {
			blogArticle.setPubdate(BlogUtil.fomat(blogArticle.getCreatTime()));
		}
		return list;
	}
}
